﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls.Window
{
    public partial class SelectUserWindow : ChildWindow
    {
        private User m_selectedUser = null;

        public User SelectedUser
        {
            get { return m_selectedUser; }
            set { m_selectedUser = value; }
        }

        private string m_username;
        private int m_orgId;
        private int m_typeCode;
        private ObservableCollection<User> m_users;

        public SelectUserWindow(string username)
        {
            m_username = username;

            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SelectUserWindow_Loaded);
        }

        public SelectUserWindow(int orgId, int typeCode)
        {
            m_orgId = orgId;
            m_typeCode = typeCode;

            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SelectUserWindow_Loaded);
        }

        void SelectUserWindow_Loaded(object sender, RoutedEventArgs e)
        {
            m_busy.IsBusy = true;

                DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
                client.GetUsersCompleted += new EventHandler<GetUsersCompletedEventArgs>(client_GetUsersCompleted);
                client.GetUsersAsync();
        }

        void client_GetUsersCompleted(object sender, GetUsersCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                m_users = e.Result.Data;

                if (SelectedUser != null)
                {
                    foreach (User user in m_users)
                    {
                        if (user.Id == SelectedUser.Id)
                        {
                            //user.IsSelected = true;
                            break;
                        }
                    }
                }

                PagedCollectionView pagedView = new PagedCollectionView(m_users);
                PropertyGroupDescription description = new PropertyGroupDescription("GroupName");
                pagedView.GroupDescriptions.Add(description);

                m_dataGridUsers.ItemsSource = pagedView;
            }
            m_busy.IsBusy = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (User user in m_users)
            {
                //if (user.IsSelected)
                //{
                //    SelectedUser = user;
                //    break;
                //}
            }

            //BUGBUG
            //如果选择admin则会出错
            //admin的UserID为0
            if (SelectedUser != null)// && SelectedUser.Id > 0)
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show("请选择用户");
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public void InitSelectedUser(User selectedUser)
        {
            SelectedUser = selectedUser;
            if (SelectedUser != null && m_users != null)
            {
                foreach (User user in m_users)
                {
                    if (user.Id == SelectedUser.Id)
                    {
                        //user.IsSelected = true;
                        break;
                    }
                }
            }
        }
    }
}

